package com.xqls.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.RolePermissionKeyBean;
import com.xqls.biz.RolePermissionKeyBiz;
import com.xqls.dal.model.RolePermissionKey;
import com.xqls.service.RolePermissionKeyService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.ObjectUtil;
import com.xqls.util.VelocityUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class RolePermissionKeyBizImpl implements RolePermissionKeyBiz {

	private static Logger logger=Logger.getLogger(RolePermissionKeyBiz.class);
	
	@Autowired private RolePermissionKeyService rolePermissionService;


	@Override
	public CommonResult delRolePerByRoleId(String roleId){
		CommonResult commonResult=new CommonResult();
		boolean success;
		success=rolePermissionService.deleteById(roleId);
		commonResult.setIsSuccess(success);
		return commonResult ;
	}


	@SuppressWarnings("unchecked")
	@Override
	public CommonResult setUpRecord(String roleId,List<RolePermissionKeyBean> beanList) {
		CommonResult commonResult=new CommonResult();
        Map<String,Object> map=new HashMap<String,Object>();
		List<RolePermissionKey> list=null;
		boolean isDelete=false;
		boolean isInsert=false;
		
		try {
			map.put("roleId", roleId);
			
			isDelete=this.rolePermissionService.deleteByMap(map);
			if(!beanList.isEmpty())
			{
				list= ObjectUtil.transferList(beanList, RolePermissionKey.class);
				isInsert=this.rolePermissionService.insertByList(list);
			}

			commonResult.setIsSuccess(isDelete || isInsert);	
		} catch (Exception e) {
			logger.error("修改角色关联权限异常", e);
		}
		return commonResult;
	}

	@Override
	public CommonResult getListByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult commonResult = new CommonResult();
		try {
			commonPage.setPageData(this.rolePermissionService.getListByMap(commonPage.pageToMap()));
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("rolePermissionList",commonPage.getPageData());
			commonPage.setHtml(VelocityUtil.getHtmlByMap("rolePermission/rolePermissionSetUp.vm",showMap,request,response,request.getSession().getServletContext()));
			commonResult.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("查询角色关联权限异常", e);
		}
		return commonResult;
	}

	@Override
	public List<Map<String, Object>> getListByRoleId(String roleId) {
		List<Map<String, Object>> list=null;
		try {
			 list = this.rolePermissionService.getListByRoleId(roleId);
		} catch (Exception e) {
			logger.error("查询角色关联权限异常", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<RolePermissionKeyBean> getList() {
		List<RolePermissionKeyBean> list=null;
		try {
			list=ObjectUtil.transferList(this.rolePermissionService.getList(),RolePermissionKeyBean.class);
		} catch (Exception e) {
			logger.error("查询角色关联权限异常", e);
		}
		return list;
	}
	
	@Override
	public Map<String,Object> getBeanByKey(String roleId,String permId) {
		List<Map<String,Object>> list =null;
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("roleId", roleId);
		map.put("permId", permId);
		
		try {
			list = this.rolePermissionService.getListByMap(map);
			if(!list.isEmpty())
			{
				map=list.get(0);
			}
		} catch (Exception e) {
			logger.error("查询角色关联权限异常", e);
		}
		
		return map;
	}

	
}
